package com.powernode.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

/**
 * security的配置类
 */
@Configuration
//@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration {

    /**
     * 构建多个内存用户
     *
     * spring 会注入passwordEncoder
     * @return
     */
    @Bean
    public InMemoryUserDetailsManager userDetailsService(BCryptPasswordEncoder passwordEncoder) {
        String encode = passwordEncoder.encode("123");
        UserDetails admin = User.builder().username("admin").password(encode).authorities("sys:save", "sys:select").build();

        UserDetails jack = User.builder().username("jack").authorities("sys:select").password(encode).build();
        System.out.println( encode);

        return new InMemoryUserDetailsManager(admin, jack);
    }


    /**
     * 配置加密器
     */
    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
